const fs = require("fs").promises;
const path = require("path");
const MAIN_PATH = path.resolve(__dirname, "..")
const XJB_PATH = path.resolve(__dirname, "..", "..", "新将包");
const importFile = [
    "css/main.css",
    "css/editor.css",
    "js/import/url.js",
    "image/xjb_close.png",
    "js/editor.js",
].map(url => {
    return path.resolve(XJB_PATH, url);
});
const importDir = [
    "js/tool",
    "js/interact",
    "js/editor"
].map(url => {
    return path.resolve(XJB_PATH, url)
});
const receiveFile = async (...files) => {
    const reads = files.map(file => {
        return fs.readFile(file);
    })
    const datas = await Promise.all(reads);
    const writes = datas.map((data, index) => {
        return fs.writeFile(files[index].replace(XJB_PATH, MAIN_PATH), data);
    })
    await Promise.all(writes)
}
const receiveDir = async (...dirs) => {
    const status = dirs.map(dir => {
        return fs.readdir(dir)
    })
    const resources = (await Promise.all(status)).map((list, index) => {
        return list.map(name => path.resolve(dirs[index], name))
    }).flat();
    const stats = resources.map(resource => {
        return fs.stat(resource)
    })
    const isFiles = (await Promise.all(stats)).map(stat => {
        return stat.isFile;
    })
    const files = resources.filter((_, index) => {
        return isFiles[index];
    })
    receiveFile(...files);
}
receiveDir(...importDir)
receiveFile(...importFile)